package com.battle.service.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.battle.pojo.entity.SearchRecord;
import com.battle.pojo.param.searchRecord.*;
import com.battle.pojo.vo.searchRecord.*;
import com.battle.pojo.dto.searchRecord.*;
import com.battle.service.repository.in.SearchRecordRepository;
import com.battle.service.service.in.SearchRecordService;
import nirvana.core.domains.vo.PageResult;
import nirvana.core.utils.ids.IdGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.List;

/**
 * 【描 述】：搜索记录服务
 * 【环 境】：J2SE 17
 * 【详 细】：
 */
@Service
public class SearchRecordServiceImpl implements SearchRecordService {

    private final SearchRecordRepository searchRecordRepository;

    public SearchRecordServiceImpl(SearchRecordRepository searchRecordRepository) {
        this.searchRecordRepository = searchRecordRepository;
    }


    /**
     * 创建搜索记录
     *
     * @param param
     */
    @Override
    public void createSearchRecord(CreateSearchRecordParam param) {
        SearchRecord searchRecord = new SearchRecord();
        BeanUtils.copyProperties(param, searchRecord);
        Long id = IdGenerator.SNOW.generate();
        searchRecord.setId(id);
        searchRecordRepository.create(searchRecord);
    }

    /**
     * 修改搜索记录
     *
     * @param param
     */
    @Override
    public void editSearchRecord(EditSearchRecordParam param) {
        Long id = param.getId();
        SearchRecord searchRecord = searchRecordRepository.getByIdCheck(id);
        BeanUtils.copyProperties(param, searchRecord);
        searchRecordRepository.edit(searchRecord);
    }

    /**
     * 移除搜索记录
     *
     * @param id
     */
    @Override
    public void removeSearchRecord(Long id) {
        searchRecordRepository.removeById(id);
    }

    /**
     * 启动/禁用搜索记录
     *
     * @param id
     */
    @Override
    public void enabledSearchRecord(Long id) {
        SearchRecord searchRecord = searchRecordRepository.getByIdCheck(id);
        searchRecord.setIsDisable(!searchRecord.getIsDisable());
        searchRecordRepository.edit(searchRecord);
    }

    /**
     * 获取搜索记录详情
     *
     * @param id
     * @return
     */
    @Override
    public GetSearchRecordVo getSearchRecord(Long id) {
        SearchRecord searchRecord = searchRecordRepository.getByIdCheck(id);
        GetSearchRecordVo vo = new GetSearchRecordVo();
        BeanUtils.copyProperties(searchRecord, vo);
        return vo;
    }

    /**
     * 获取搜索记录 列表
     *
     * @param param
     * @return
     */
    @Override
    public PageResult<GetSearchRecordListVo> getSearchRecordList(GetSearchRecordListParam param) {

        SelectSearchRecordDto dto = new SelectSearchRecordDto();
        BeanUtils.copyProperties(param, dto);
        Page<SearchRecord> page = searchRecordRepository.selectSearchRecord(dto);
        List<SearchRecord> searchRecordList = page.getRecords();
        List<GetSearchRecordListVo> list = new ArrayList<>();
        for (SearchRecord searchRecord : searchRecordList) {
            GetSearchRecordListVo vo = new GetSearchRecordListVo();
            BeanUtils.copyProperties(searchRecord, vo);

            list.add(vo);
        }
        return new PageResult<>(list, page);

    }

    /**
     * 获取搜索记录下拉列表
     *
     * @param param
     * @return
     */
    @Override
    public List<GetSearchRecordDownListVo> getSearchRecordDownList(GetSearchRecordDownListParam param) {
        SearchRecord po = new SearchRecord();
        if (param != null) {
            BeanUtils.copyProperties(param, po);
            po.setIsDelete(false);
        }
        List<SearchRecord> searchRecordList = searchRecordRepository.getListByEntity(po);
        //根据创建时间倒序排序
        searchRecordList.sort((o1, o2) -> o2.getCreateTime().compareTo(o1.getCreateTime()));
        List<GetSearchRecordDownListVo> list = new ArrayList<>();
        for (SearchRecord searchRecord : searchRecordList) {
            GetSearchRecordDownListVo vo = new GetSearchRecordDownListVo();
            BeanUtils.copyProperties(searchRecord, vo);
            list.add(vo);
        }
        return list;
    }


}



